package edu.fhsu.csci468;

import javax.swing.event.EventListenerList;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.io.IOException;


public class ControlPanelThread implements Runnable, IHandlerEventListener
{

	private EventListenerList listenerList = new EventListenerList();
	private ObjectOutputStream outStream;
	private ObjectInputStream inStream;
	private Socket incoming;
	
	public ControlPanelThread(Socket incoming)
	{
		this.incoming = incoming;
		
		try
		{
		outStream = new ObjectOutputStream(new BufferedOutputStream(
				incoming.getOutputStream()));
		outStream.flush();
		
		outStream.writeObject(new 
				ControlPanelDisplayMessage("Welcome to SafeHome\nPress 0 for Menu"));
		outStream.flush();

		inStream = new ObjectInputStream(new BufferedInputStream(
				incoming.getInputStream()));

		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void serverEventOccurred(ServerEvent evt) 
	{
		try
		{
			outStream.writeObject(new 
					ControlPanelDisplayMessage(evt.getMessage()));
			outStream.flush();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void sendMessage(String message)
	{
		try
		{
		outStream.writeObject(new 
				ControlPanelDisplayMessage(message));
		outStream.flush();		
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	public void run()
	{
		System.out.println("Control Panel Thread up.");
		Integer receive = -1;
		do
		{
			try
			{
				receive = (Integer) inStream.readObject();
			} catch (ClassNotFoundException classNotFoundException)
			{
				System.out.println(classNotFoundException.getCause());
				receive = -1;
			} catch (IOException e)
			{
				receive = -1;
				e.printStackTrace();
			}

			fireControlPanelEvent(new ControlPanelEvent(this, receive));

		}while(receive != -1);
		
		try
		{
		outStream.writeObject(new ControlPanelDisplayMessage(false));
		outStream.flush();
		incoming.close();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}

		
	}
	
	public void addControlPanelEventListener(
			IControlPanelEventListener eventListener)
	{
		listenerList.add(IControlPanelEventListener.class, eventListener);
	}

	public void removeControlPanelEventListener(
			IControlPanelEventListener eventListener)
	{
		listenerList.remove(IControlPanelEventListener.class, eventListener);
	}

	public void removeAllControlPanelEventListeners()
	{
		listenerList = new EventListenerList();
	}

	private void fireControlPanelEvent(ControlPanelEvent event)
	{
		if(listenerList != null)
		{
			Object[] listeners = listenerList.getListenerList();
			// Each listener occupies two elements - the first is the listener class
			// and the second is the listener instance
			for (int i = 0; i < listeners.length; i += 2)
			{
				if (listeners[i] == IControlPanelEventListener.class)
				{
					((IControlPanelEventListener) listeners[i + 1])
					.controlPanelEventOccurred(event);
				}
			}
		}
	}
}
